import Category from "../../api/modules/Category";

export default {
    namespaced: true,
    state: {
        category: [], // 所有分类列表 [{id:number,parent_id:number,name:string}]
    },
    getters: {},
    mutations: {
        setCategory(state, val = []) {
            state.category = val || [];
        },
    },
    actions: {
        /**
         * 获取所有分类页表
         * @param commit
         * @param state
         * @param force {boolean} 是否强制刷新
         * @returns {Promise<[]>}
         */
        async getAll({commit, state}, force = false) {
            if (force || state.category.length === 0) {
                try {
                    const {data} = await Category.all();
                    commit("setCategory", data);
                } catch (e) {

                }
            }
            return state.category;
        }
    },
};

